<?php
class LogsModel
{
    public function __construct()
    {
        $this->db = Yaf_Registry::get('db');
        $this->time = date("Y-m-d H:i:s",time());
    }

    public function selectLoginLogs($start,$everyPage)
    {
        $sql="select * from login_log order by id desc limit {$start},{$everyPage}";
        return $this->db->query($sql);
    }

    public function getUserLogsInfo($start,$everyPage)
    {
        $sql="select * from user_log order by id desc limit {$start},{$everyPage}";
        return $this->db->query($sql);
    }

    public function getUserAllCount()
    {
        $sql="select count(*) as `rows` from `user_log` ";
        return $this->db->query($sql);
    }

    public function getCollAllCount()
    {
        $sql="select count(*) as `rows` from `collection_log` ";
        return $this->db->query($sql);
    }

    public function getLoginAllCount()
    {
        $sql="select count(*) as `rows` from `login_log` ";
        return $this->db->query($sql);
    }

    public function getPushAllCount()
    {
        $sql="select count(*) as `rows` from `push_log` ";
        return $this->db->query($sql);
    }

    //添加用户登录日志
    public function addLogin($info)
    {
        $sql="insert into login_log(`user_id`,`ip`,`create_time`) values('{$info[0]['user_id']}','{$info[0]['ip']}',UNIX_TIMESTAMP('{$this->time}'))";
        return $this->db->query($sql);
    }

   //添加日志
    public function addUser($info)
    {
        $sql="insert into user_log(`table_name`,`user_id`,`action`,`create_time`) VALUES ('{$info[0]["table_name"]}','{$info[0]["user_id"]}','{$info[0]["action"]}',UNIX_TIMESTAMP('{$this->time}'))";
        return $this->db->query($sql);
    }

    //删除用户日志
    public function delUser($id)
    {
        $sql = "delete from user_log where id='{$id}'";
        return $this->db->query($sql);
    }
    //编辑用户日志
    public function editUser($info, $id)
    {
        $sql = "update `user_log` set `table_name`='{$info['table']}', `action`='{$info['action']}' WHERE `id`='{$id}'";
        return $this->db->query($sql);
    }

    public function selectCollLogs()
    {
        $sql="select * from collection_log";
        return $this->db->query($sql);
    }

    public function getCollLogsInfo($start,$everyPage)
    {
        $sql="select * from collection_log order by id desc limit {$start},{$everyPage}";
        return $this->db->query($sql);
    }
    //添加采集日志
    public function addColl($info)
    {

        $sql="insert into collection_log(`collection_robot_id`,`collection_source_id`,`url`,`article_id`,`create_time`) values('{$info['collection_robot_id']}','{$info['collection_source_id']}','{$info["url"]}','{$info['article_id']}',UNIX_TIMESTAMP('{$this->time}'))";
        return $this->db->query($sql);
    }
    public function selectPushLogs()
    {
        $sql="select * from push_log";
        return $this->db->query($sql);
    }

    public function getPushLogsInfo($start,$everyPage)
    {
        $sql="select siteid,catid,article_title,user_id,status,create_time from push_log order by id desc limit {$start},{$everyPage}";
        return $this->db->query($sql);
    }
    //删除采集日志
    public function delColl($id)
    {
        $sql = "delete from collection_log where id='{$id}'";
        return $this->db->query($sql);
    }
    //编辑采集日志
    public function editColl($info, $id)
    {
        $sql = "update `collection_log` set `collection_robot_id`='{$info['robot']}', `collection_source_id`='{$info['source']}',`url`='{$info['url']}',`article_id`='{$info['article']}',`create_time`=UNIX_TIMESTAMP('{$info['date']}') WHERE `id`='{$id}'";
        return $this->db->query($sql);
    }
    //添加推送日志
    public function addPush($info)
    {
        $sql="insert into push_log(`content_id`,`user_id`,`push_time`,`status`,`create_time`) values('{$info['article']}','{$info['pusher']}',UNIX_TIMESTAMP('{$info['date']}'),'{$info['status']}','time()')";
        //var_dump($sql);
        return $this->db->query($sql);
    }
    //删除推送日志
    public function delPush($id)
    {
        $sql = "delete from push_log where id='{$id}'";
        return $this->db->query($sql);
    }
    //编辑推送日志
    public function editPush($info, $id)
    {
        $sql = "update `push_log` set `article_id`='{$info['article']}', `user_id`='{$info['pusher']}',`push_time`='{$info['time']}',`status`='{$info['status']}' WHERE `id`='{$id}'";
        return $this->db->query($sql);
    }


    //查询该条日志是否存在，
    public function selectExits($userid)
    {
        $sql="select * from login_log where user_id='{$userid}'";
        //echo $sql;
        return $this->db->query($sql);
    }

    //删除用户登录日志
    public function delLogin($id)
    {
        $sql = "delete from login_log where id='{$id}'";
        return $this->db->query($sql);
    }
    //编辑用户登录日志
    public function editLogin($info, $id)
    {
        $sql = "update `login_log` set `user_id`='{$info['username']}', `ip`='{$info['ip']}',`create_time`=UNIX_TIMESTAMP('{$info['date']}') WHERE `id`='{$id}'";
        return $this->db->query($sql);
    }
    public function select()
    {
        //table_name
        $sql="show columns from user_log like 'action'";
        return $this->db->select($sql);
    }
    public function selectTable()
    {
        $sql="show columns from user_log like 'table_name'";
        return $this->db->select($sql);
    }
    //修改采集的时间
    public function updateCollTime($start,$end,$id)
    {
        $sql="update `collection_log` set `start_time`=UNIX_TIMESTAMP('{$start}'),`end_time`=UNIX_TIMESTAMP('{$end}') where Id='{$id}'";
        return $this->db->query($sql);
    }

    public function selectPushTime($info,$user)
    {
        $i        = 0;
        $temp     = 0;
        $result   = array();
        $res      = array();
        $next_day = strtotime($info['date']) + 3600*24;
        if($info['user_id'] == 0){
            foreach ($user as $key => $value) {    
                $sql="select max(create_time) as time,user_id,siteid  from  `push_log` where user_id = '{$value['user_id']}' and `create_time`> UNIX_TIMESTAMP('{$info['date']}') and `create_time`<'{$next_day}'";
                if($this->db->query($sql)[0]['user_id'] != NULL){
                    $result[$i++] = $this->db->query($sql)[0];
                }     
            }
        }
        else{
            $sql="select max(create_time) as time,user_id,siteid  from  `push_log` where user_id = '{$info['user_id']}' and `create_time`> UNIX_TIMESTAMP('{$info['date']}') and `create_time`<'{$next_day}'";
            $result[$i++] = $this->db->query($sql)[0];
        }
        

        return $result;
    }

}